package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.mapper.NewsArticleMapper;
import com.woniuxy.pojo.NewsArticle;
import com.woniuxy.pojo.dto.NewsArticleDto;
import com.woniuxy.pojo.vo.AuditVO;
import com.woniuxy.service.NewsArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Xi'ao'M'ing
* @description 针对表【t_news_article(资讯表)】的数据库操作Service实现
* @createDate 2025-06-04 11:24:02
*/
@Service
public class NewsArticleServiceImpl extends ServiceImpl<NewsArticleMapper, NewsArticle>
    implements NewsArticleService {

    @Autowired
    private NewsArticleMapper newsArticleMapper;

    @Override
    public Page<NewsArticle> getNewsByType(Integer typeId, Integer pageNum, Integer pageSize, NewsArticleDto dto) {
        LambdaQueryWrapper<NewsArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsArticle::getTypeId,typeId);
        wrapper.orderByDesc(NewsArticle::getTopLevel);
        if (dto != null) {
            if (dto.getAuthor() != null) {
                wrapper.eq(NewsArticle::getAuthor, dto.getAuthor());
            }
            if (dto.getStatus() != null) {
                wrapper.eq(NewsArticle::getStatus, dto.getStatus());
            }
            if (dto.getTitle() != null && !dto.getTitle().isEmpty()) {
                wrapper.like(NewsArticle::getTitle, dto.getTitle());
            }
        }
        Page<NewsArticle> page = new Page<>(pageNum,pageSize);
        return newsArticleMapper.selectPage(page, wrapper);

    }

    @Override
    public List<NewsArticle> getAuthors() {
        LambdaQueryWrapper<NewsArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(NewsArticle::getAuthor);
        List<NewsArticle> newsArticles = newsArticleMapper.selectList(wrapper);
        return newsArticles;
    }

    @Override
    public Page<AuditVO> getNewsByStatus(Integer pageNum, Integer pageSize, NewsArticleDto dto) {
        Page<AuditVO> page = new Page<>(pageNum,pageSize);
        return newsArticleMapper.getNewsByStatus(page,dto);
    }
}




